# Definition for a binary tree node.
from typing import Optional


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
        
class Solution:
    def sumRootToLeaf(self, root: Optional[TreeNode]) -> int:
        ans = 0
        def dfs(node, path):
            nonlocal ans
            if not node:
                return
            path = (path << 1) + node.val
            if not node.left and not node.right:
                ans += path
            dfs(node.left, path)
            dfs(node.right, path)
        dfs(root, 0)
        return ans